package com.shycloud.mido.organ.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.MemberDetailInfo;
import com.shycloud.mido.organ.api.dto.MiniInteractionDTO;
import com.shycloud.mido.organ.api.entity.MemberDetailInfoEntity;
import com.shycloud.mido.organ.api.entity.MemberEntity;
import com.shycloud.mido.organ.api.entity.MiniDiaryZan;
import com.shycloud.mido.organ.api.entity.MiniFollowed;
import com.shycloud.mido.organ.api.entity.MiniInteraction;
import com.shycloud.mido.organ.api.entity.MiniMessage;
import com.shycloud.mido.organ.api.entity.MiniWorksZan;
import com.shycloud.mido.organ.api.entity.WorksEntity;
import com.shycloud.mido.organ.mapper.CommunityMapper;
import com.shycloud.mido.organ.mapper.InteractionMapper;
import com.shycloud.mido.organ.mapper.MemberDetailInfoMapper;
import com.shycloud.mido.organ.mapper.MemberMapper;
import com.shycloud.mido.organ.mapper.MessageMapper;
import com.shycloud.mido.organ.mapper.MiniDiaryZanMapper;
import com.shycloud.mido.organ.mapper.MiniFollowedMapper;
import com.shycloud.mido.organ.mapper.MiniWorksZanMapper;
import com.shycloud.mido.organ.mapper.WorksMapper;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.db.sql.Query;
import lombok.AllArgsConstructor;

@RestController
@AllArgsConstructor
@RequestMapping("/community")
public class CommunityController {

	private WorksMapper worksMapper;
	private MiniFollowedMapper followedMapper;
	private MiniDiaryZanMapper zanDiaryMapper;
	private MiniWorksZanMapper zanWorksMapper;
	private MemberMapper memberMapper;
	private MemberDetailInfoMapper memberDetailMapper;
	private MessageMapper messageMapper;
	private InteractionMapper interactionMapper;
	private CommunityMapper communityMapper;

	//用户关系模块

	//我的个人页面详情
	@GetMapping("/getMemberDetail")
	public R getMemberDetail(String memberId) {
		Map<String, Object> result = new HashMap<String, Object>();

		QueryWrapper queryFollow = new QueryWrapper<>();
		queryFollow.eq("member_id", memberId);
		Integer follow = followedMapper.selectCount(queryFollow);

		QueryWrapper queryFollowed = new QueryWrapper<>();
		queryFollowed.eq("to_member_id", memberId);
		Integer followed = followedMapper.selectCount(queryFollowed);
		//我关注的数量
		result.put("follow", follow);
		//粉丝数量
		result.put("followed", followed);
		//作品全部id
		List<String> ids = new ArrayList<String>();
		ids = worksMapper.getWorksIdList(memberId, 0);
		result.put("worksIdList", ids);
		//是否有未读系统消息
		QueryWrapper queryMessage = new QueryWrapper<>();
		queryMessage.eq("member_id", memberId);
		queryMessage.eq("status", 0);
		int messageNum = messageMapper.selectCount(queryMessage);
		if (messageNum > 0) {
			result.put("messageStatus", true);
		} else {
			result.put("messageStatus", false);
		}
		//是否有未读互动消息
		QueryWrapper queryInteraction = new QueryWrapper<>();
		queryInteraction.eq("to_member_id", memberId);
		queryInteraction.eq("status", 0);
		int interactionNum = interactionMapper.selectCount(queryInteraction);
		if (interactionNum > 0) {
			result.put("interactionStatus", true);
		} else {
			result.put("interactionStatus", false);
		}
		return new R<>(result);
	}

	//获取他人详情(粉丝数量, 关注数量, 性别)
	@GetMapping("/getOtherMemberDetail")
	public R getOtherMemberDetail(String myMemberId, String otherMemberId) {
		Map<String, Object> result = new HashMap<String, Object>();
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id ", otherMemberId);
		MemberDetailInfoEntity member = memberDetailMapper.selectOne(queryWrapper);
		result.put("sex", member.getSex());
		QueryWrapper queryFollow = new QueryWrapper<>();
		queryFollow.eq("member_id", otherMemberId);
		Integer follow = followedMapper.selectCount(queryFollow);

		QueryWrapper queryFollowed = new QueryWrapper<>();
		queryFollowed.eq("to_member_id", otherMemberId);
		Integer followed = followedMapper.selectCount(queryFollowed);
		//我关注的数量
		result.put("follow", follow.toString());
		//粉丝数量
		result.put("followed", followed.toString());
		//关注状态(是否已关注)
		QueryWrapper queryFollowStatus = new QueryWrapper<>();
		queryFollowStatus.eq("member_id", myMemberId);
		queryFollowStatus.eq("to_member_id", otherMemberId);
		if (followedMapper.selectCount(queryFollowStatus) == 0) {
			result.put("followStatus", "0");
		} else {
			result.put("followStatus", "1");
		}
		List<String> ids = new ArrayList<String>();
		if (isFriend(myMemberId, otherMemberId)) {
			ids = worksMapper.getWorksIdList(otherMemberId, 1);
		} else {
			ids = worksMapper.getWorksIdList(otherMemberId, 2);
		}
		result.put("worksIdList", ids);
		return new R<>(result);
	}

	//我的消息
	@GetMapping("/getMyMessage")
	public R getMyMessage(Page<MiniMessage> page, String memberId) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		queryWrapper.eq("member_id", memberId);
		return new R<>(messageMapper.selectPage(page, queryWrapper));
	}

	//将所有消息置为已读
	@GetMapping("/checkAllMessage")
	public R checkAllMessage(String memberId) {
		MiniMessage entity = new MiniMessage();
		entity.setStatus(1);
		QueryWrapper updateWrapper = new QueryWrapper<>();
		updateWrapper.eq("member_id", memberId);
		return new R<>(messageMapper.update(entity, updateWrapper));
	}

	//我的收藏
	@GetMapping("/getMyZan")
	public R getMyZan(Page<WorksEntity> page, String memberId) {
		return new R<>(communityMapper.getMyZan(page, memberId));
	}

	//我的关注
	@GetMapping("/getMyFollow")
	public R getMyFollow(Page<MemberEntity> page, String memberId) {
		return new R<>(communityMapper.getMyFollow(page, memberId));
	}

	//将某一条互动消息置为已读
	@GetMapping("/checkOneInteraction")
	public R checkOneInteraction(String targetId) {

		List<MiniInteraction> miniInteractions = interactionMapper.selectList(new QueryWrapper<MiniInteraction>().eq("target_id", targetId));

		miniInteractions.forEach(x -> {
			x.setStatus(1);
			interactionMapper.updateById(x);
		});

		return new R<>(miniInteractions.size());
	}


	//我的互动
	@GetMapping("/getMyInteraction")
	public R getMyInteraction(Page<MiniInteractionDTO> page, String memberId) {
		return new R<>(communityMapper.getMyInteraction(page, memberId));
	}


	//关注/点赞需要增加去重逻辑
	@GetMapping("/followOne")
	public R followOne(String memberId, String toMemberId) {

		if (memberId.equals(toMemberId)) {
			return new R<>(CommonConstants.FAIL, "不能关注自己", false);
		}

		int result = 0;
		MiniFollowed followed = new MiniFollowed();
		followed.setMemberId(memberId);
		followed.setToMemberId(toMemberId);
		//查询是否已经点过赞
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id", memberId);
		queryWrapper.eq("to_member_id", toMemberId);
		if (followedMapper.selectCount(queryWrapper) == 0) {
			result = followedMapper.insert(followed);
		}
		return new R<>(result);
	}

	@GetMapping("/cancleFollow")
	public R cancleFollow(String memberId, String toMemberId) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id", memberId);
		queryWrapper.eq("to_member_id", toMemberId);
		int result = followedMapper.delete(queryWrapper);
		return new R<>(result);
	}

	@GetMapping("/zanDiary")
	public R zanDiary(String memberId, String memberName, String diaryId) {
		int result = 0;
		MiniDiaryZan zan = new MiniDiaryZan();
		zan.setDiaryId(diaryId);
		zan.setMemberId(memberId);
		zan.setMemberName(memberName);
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("diary_id", diaryId);
		queryWrapper.eq("member_id", memberId);
		if (zanDiaryMapper.selectCount(queryWrapper) == 0) {
			result = zanDiaryMapper.insert(zan);
		}
		return new R<>(result);
	}

	@GetMapping("/cancleZanDiary")
	public R cancleZanDiary(String memberId, String diaryId) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id", memberId);
		queryWrapper.eq("diary_id", diaryId);
		int result = zanDiaryMapper.delete(queryWrapper);
		return new R<>(result);
	}

	@GetMapping("/zanWorks")
	public R zanWorks(String memberId, String memberName, String worksId) {
		int result = 0;
		MiniWorksZan zan = new MiniWorksZan();
		zan.setWorksId(worksId);
		zan.setMemberId(memberId);
		zan.setMemberName(memberName);
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("works_id", worksId);
		queryWrapper.eq("member_id", memberId);
		if (zanWorksMapper.selectCount(queryWrapper) == 0) {
			result = zanWorksMapper.insert(zan);
		}
		return new R<>(result);
	}

	@GetMapping("/cancleZanWorks")
	public R cancleZanWorks(String memberId, String worksId) {
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("member_id", memberId);
		queryWrapper.eq("works_id", worksId);
		int result = zanWorksMapper.delete(queryWrapper);
		return new R<>(result);
	}

	//判断是否为好友
	public Boolean isFriend(String myMemberId, String otherMemberId) {
		//互相关注即为好友
		QueryWrapper from = new QueryWrapper<>();
		from.eq("member_id", otherMemberId);
		from.eq("to_member_id", myMemberId);
		Integer fromNum = followedMapper.selectCount(from);

		QueryWrapper to = new QueryWrapper<>();
		to.eq("member_id", myMemberId);
		to.eq("to_member_id", otherMemberId);
		Integer toNum = followedMapper.selectCount(to);

		if (fromNum != 0 && toNum != 0) {
			return true;
		} else {
			return false;
		}
	}
}
